<?php
class KomentarzeModel{
	protected static function uprawnienia(){
		global $objSession;
		if ( $objSession->IsLoggedIn() && $objSession->GetUserData("stanowisko") == 'a') 
		tools::_assign("uprawnienia",1);
	}
	
	protected function pobierzNumerKomentowanegoObiektu(){
		global $config;
		$q__ = "SELECT COUNT(*) FROM ".KMCATEGO." a 
			WHERE  a.kmPageID = '{$this->kmPageID}' AND kmTematID = '{$this->kmTematID}' AND kmWpisID = '{$this->kmWpisID}'";
		$kmcIDstan = tools::__adodb('getOne',$q__,true,$this->db);
		
		if ($kmcIDstan >= 1 ){
			$q__ = "SELECT kmcID FROM ".KMCATEGO." a 
			WHERE  a.kmPageID = '{$this->kmPageID}' AND kmTematID = '{$this->kmTematID}' AND kmWpisID = '{$this->kmWpisID}'";
			return tools::__adodb('getOne',$q__,true,$this->db);
		}else{
			$q__ = "INSERT INTO ".KMCATEGO." (kmcID, kmPageID, kmTematID, kmWpisID) 
				VALUES (NULL, '{$this->kmPageID}','{$this->kmTematID}','{$this->kmWpisID}')";
			tools::__adodb('exec',$q__,true,$this->db);
			return $this->db->lastInsertId();
		}
	}
		
	protected function pobierzListeKomentarzy(){
		$q__ = "SELECT * FROM ".KMEWPISY." a 
			WHERE  a.kmcID = '{$this->kmcID}'";
		return tools::__adodb('getAll',$q__,true,$this->db);
	}
	
	protected function zapiszNowyKomentarz(){
		global $config;
		$q__ = "SELECT COUNT(*) FROM ".KMCATEGO." a 
			WHERE  a.kmcID = '{$config->kmcID}'";
		$istnieje = tools::__adodb('getOne',$q__,true,$this->db);

		if ( $istnieje == 1 ) {			
			$q__ = "INSERT INTO ".KMEWPISY." (kmwID, kmcID, kmwNazwa, kmwWpis, kmwData) 
				VALUES (NULL, '{$config->kmcID}','{$config->kmwNazwa}','{$config->kmwWpis}','".date('Y-m-d H:i:s')."')";
			tools::__adodb('exec',$q__,true,$this->db);
		}
	}
	
	protected function usunWpisKomentarza($kmwID, $kmcID){
		$q__ = "DELETE FROM ".KMEWPISY." WHERE kmwID = '{$kmwID}' AND kmcID = '{$kmcID}'";
		tools::__adodb('exec',$q__,true,$this->db);
	}
}